package wsi.gui;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import org.rygh.core.util.UIo;
import org.rygh.core.util.UTime;

import wsi.function.Function;
import wsi.function.FunctionController;
import wsi.script.AppConfig;
import wsi.ws.WebServices;

public class Property {

	enum Key {
		RECENT_FUNCTION,
	}

	private static Property property = null;
	private static final String PROPERTY_FILE = "app.properties";
	private static final String SEPARATOR = ";";
	private static final int FUNCTION_RECORD_LIMIT = 12;

	public static Property shared() {
		if (property == null)
			property = new Property();

		return property;
	}

	private final Properties properties = new Properties();

	private Property() {
		try {
			File file = new File(AppConfig.shared().getResourcesFolder(),
					PROPERTY_FILE);

			if (!file.exists())
				UIo.saveFile(file, "");

			FileInputStream file_in = new FileInputStream(file);

			properties.load(file_in);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public List<Function> getLastestFunctions() {
		List<Function> list = new ArrayList<>();

		String val = properties.getProperty(Key.RECENT_FUNCTION.name());
		// System.out.println("getLastestFunctions " + val);

		if (val != null) {
			String[] values = val.split(SEPARATOR);
			Set<Function> set = new HashSet<>();

			for (String value : values) {
				if (value.trim().isEmpty())
					continue;

				String service, func_name;
				int pos = value.lastIndexOf('_');
				assert pos != -1 : value;

				service = value.substring(0, pos);
				func_name = value.substring(pos + 1);

				for (WebServices s : WebServices.values()) {
					if (!s.name().equals(service))
						continue;

					Function f = FunctionController.shared().getFunction(
							service, func_name);
					assert f != null : s + "/" + func_name;

					if (!set.contains(f)) {
						set.add(f);
						list.add(f);
					}
				}

				if (list.size() > FUNCTION_RECORD_LIMIT)
					break;
			}
		}

		return list;
	}

	public void recordFunction(Function function) {
		if (function == null)
			return;

		List<Function> functions = getLastestFunctions();

		boolean repeat = false;

		for (Function f : functions)
			if (f.equals(function)) {
				functions.remove(f);
				functions.add(0, f);
				repeat = true;
				break;
			}

		if (!repeat)
			functions.add(0, function);

		StringBuilder sbuilder = new StringBuilder();

		int c = Math.min(FUNCTION_RECORD_LIMIT, functions.size());

		for (int i = 0; i < c; i++) {
			Function f = functions.get(i);

			sbuilder.append(String.format("%s%s", f.getFullName(), SEPARATOR));
		}

		properties.put(Key.RECENT_FUNCTION.name(), sbuilder.toString());
		save();
	}

	public void save() {
		File file = new File(AppConfig.shared().getResourcesFolder(),
				PROPERTY_FILE);

		try {
			FileOutputStream out = new FileOutputStream(file);
			properties.store(out, UTime.toLogString());
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
